package com.apanal.qlife.app.controller;

import java.text.ParseException;
import java.util.Date;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.apanal.qlife.advice.model.Advice;
import com.apanal.qlife.advice.service.SysAnnounceService;
import com.apanal.qlife.advice.service.SysInformService;
import com.apanal.qlife.app.model.SoftwareVer;
import com.apanal.qlife.app.service.AppService;
import com.apanal.qlife.common.annotation.CurrentUser;
import com.apanal.qlife.common.annotation.LogType;
import com.apanal.qlife.common.annotation.SystemLog;
import com.apanal.qlife.common.constants.AdviceConstants;
import com.apanal.qlife.common.constants.Constants;
import com.apanal.qlife.common.page.Page;
import com.apanal.qlife.common.page.PageReturn;
import com.apanal.qlife.common.util.IdWorkerFactory;
import com.apanal.qlife.sys.model.Records;
import com.apanal.qlife.sys.model.User;

@Controller
@RequestMapping("/appManager")
public class AppManagerController {

	@Autowired
	private SysAnnounceService sysAnnounceservice;

	@Autowired
	private AppService appService;

	@Autowired
	private SysInformService sysInformService;

	/**
	 * app公告查询已发布
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app已发布公告", type = LogType.SELECT)
	@RequiresPermissions("app:appAnnouce:view")
	@RequestMapping(value = "appAnnouceList")
	public String appAnnouceList(Model model, Advice advice, Page page) {
		// advice.setAdvType(3);
		advice.setAdvType(AdviceConstants.APP_ANNOUNCE);
		PageReturn<Advice> pradvice = sysAnnounceservice.findAppAnnouceList(
				advice, page);
		model.addAttribute("result", pradvice);
		model.addAttribute("advice", advice);
		return "app/appAnnouceList";

	}

	/**
	 * app公告查询未发布
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app未发布公告", type = LogType.SELECT)
	@RequiresPermissions("app:appAnnouce:view")
	@RequestMapping(value = "appAnnouceListNo")
	public String appAnnouceListNo(Model model, Advice advice, Page page) {
		// advice.setAdvType(3);
		advice.setAdvType(AdviceConstants.APP_ANNOUNCE);
		PageReturn<Advice> pradvice = sysAnnounceservice.findAppAnnouceListNo(
				advice, page);
		model.addAttribute("result", pradvice);
		model.addAttribute("advice", advice);
		return "app/appAnnouceListNo";

	}

	/**
	 * app公告详情查看
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appAnnouce:view")
	@RequestMapping(value = "/{advId}/appAnnouceDtail")
	public String appAnnouceDtail(Model model, Advice advice, Page page) {
		Advice ad = sysAnnounceservice.findAppAnnouceDtail(advice);
		model.addAttribute("advice", ad);
		return "app/appAnnouceDtail";

	}

	/**
	 * app公告新增
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appAnnouce:create")
	@RequestMapping(value = "appAnnouceAdd")
	public String appAnnouceAdd(Model model, Advice advice, Page page) {

		return "app/appAnnouceAdd";
	}

	/**
	 * app公告新增保存
	 * 
	 * @param model
	 * @return
	 * @throws ParseException
	 */
	@SystemLog(module = "app管理", operation = "新增app公告", type = LogType.CREATE)
	@RequiresPermissions("app:appAnnouce:create")
	@ResponseBody
	@RequestMapping(value = "appAnnouceAddSave")
	public String appAnnouceAddSave(@CurrentUser User user, Model model,
			Advice advice, Page page) throws ParseException {
		sysAnnounceservice.insertAppAdvice(advice, user);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 编辑app通知
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appAnnouce:update")
	@RequestMapping(value = "/{advId}/appAnnouceEdit")
	public String appAnnouceEdit(@CurrentUser User user, Model model,
			Advice advice) {
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		if (ac.getSetType() == 2) { // 按地域投放
			ac.setOrgIds(ac.getAeraIds());
		} else if (ac.getSetType() == 1 && ac.getSetTypeDetail() == 15) { // 按社区投放
			ac.setOrgIds(ac.getCommIds());
		}
		model.addAttribute("advice", ac);
		return "app/appAnnouceEdit";

	}

	/**
	 * 编辑app通知保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "修改app公告", type = LogType.UPDATE)
	@RequiresPermissions("app:appAnnouce:update")
	@ResponseBody
	@RequestMapping(value = "appAnnouceEditSave", method = RequestMethod.POST)
	public String appAnnouceEditSave(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		sysAnnounceservice.updateAppAonnce(ac, user, advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * app公告发送
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "发送app公告", type = LogType.UPDATE)
	@RequiresPermissions("app:appAnnouce:send")
	@RequestMapping(value = "/{advId}/appAnnouceSend")
	public String appAnnouceSend(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		ac.setStartDate(new Date());
		ac.setLastUpdateBy(user.getUseId());
		// ac.setLastUpdateDate(new Date());
		sysAnnounceservice.modifyAppAonnce(ac);
		return "redirect:/appManager/appAnnouceList";
	}

	/**
	 * app通知查询已发布
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app已发布通知", type = LogType.SELECT)
	@RequiresPermissions("app:appInform:view")
	@RequestMapping(value = "appInformList")
	public String appInformList(Model model, Advice advice, Page page) {
		// advice.setAdvType(4);
		advice.setAdvType(AdviceConstants.APP_INFORM);
		PageReturn<Advice> pradvice = sysAnnounceservice.findAppAnnouceList(
				advice, page);
		model.addAttribute("result", pradvice);
		model.addAttribute("advice", advice);
		return "app/appInformList";

	}

	/**
	 * app通知查询未发布
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "分页查询app未发布通知", type = LogType.SELECT)
	@RequiresPermissions("app:appInform:view")
	@RequestMapping(value = "appInformListNo")
	public String appInformListNo(Model model, Advice advice, Page page) {
		// advice.setAdvType(4);
		advice.setAdvType(AdviceConstants.APP_INFORM);
		PageReturn<Advice> pradvice = sysAnnounceservice.findAppAnnouceListNo(
				advice, page);
		model.addAttribute("result", pradvice);
		model.addAttribute("advice", advice);
		return "app/appInformListNo";

	}

	/**
	 * app通知新增
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appInform:create")
	@RequestMapping(value = "appInformAdd")
	public String appInformAdd(Model model, Advice advice, Page page) {

		return "app/appInformAdd";
	}

	/**
	 * app通知新增保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "新增app通知", type = LogType.CREATE)
	@RequiresPermissions("app:appInform:create")
	@ResponseBody
	@RequestMapping(value = "appInformAddSave")
	public String appInformAddSave(@CurrentUser User user, Model model,
			Advice advice, Page page) throws ParseException {

		sysInformService.insertAppAdvice(advice, user);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * app通知编辑
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appInform:update")
	@RequestMapping(value = "/{advId}/appInformEdit")
	public String appInformEdit(@CurrentUser User user, Model model,
			Advice advice) {
		Advice ac = sysAnnounceservice.findAppAnnouceDtail(advice);
		if (ac.getSetType() == 2) { // 按地域投放
			ac.setOrgIds(ac.getAeraIds());
		} else if (ac.getSetType() == 1 && ac.getSetTypeDetail() == 15) { // 按社区投放
			ac.setOrgIds(ac.getCommIds());
		}
		model.addAttribute("advice", ac);
		return "app/appInformEdit";

	}

	/**
	 * 编辑app通知保存
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "修改app通知", type = LogType.UPDATE)
	@RequiresPermissions("app:appInform:update")
	@ResponseBody
	@RequestMapping(value = "appInformEditSave", method = RequestMethod.POST)
	public String appInformEditSave(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {
		// 更新公告表
		Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		sysInformService.updateAppAonnce(ac, user, advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * app通知详情查看
	 * 
	 * @param model
	 * @return
	 */
	@RequiresPermissions("app:appInform:view")
	@RequestMapping(value = "/{advId}/appInformDtail")
	public String appInformDtail(@CurrentUser User user, Model model,
			Advice advice, Page page) {
		// 查看当前登录人是否首次访问
		int a = sysInformService
				.findReadLog(user.getUseId(), advice.getAdvId());
		Advice enty = sysInformService.findAnnounceById(advice.getAdvId());
		if (a == 0) {
			// records标志为已读，数据库加1
			Records rc = new Records();
			rc.setForeignId(advice.getAdvId());
			// rc.setInsertDate(new Date());
			// rc.setRecFlag(1);
			rc.setRecFlag(Constants.REC_FLAG_READ);
			// rc.setRecType(4);
			rc.setRecType(Constants.REC_TYPE_APP_INFORM);
			rc.setUseId(user.getUseId());
			// rc.setLastUpdateDate(new Date());
			rc.setRecId(IdWorkerFactory.nextId());
			sysInformService.insertRecords(rc);
			enty.setTotalCount(enty.getTotalCount() + 1);
			sysInformService.updateTotalCount(enty);
		}
		Advice ad = sysAnnounceservice.findAppAnnouceDtail(advice);
		model.addAttribute("advice", ad);
		return "app/appInformDtaill";

	}

	/**
	 * app通知发送
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "发送app通知", type = LogType.UPDATE)
	@RequiresPermissions("app:appInform:send")
	@RequestMapping(value = "/{advId}/appInformSend")
	public String appInformSend(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		ac.setStartDate(new Date());
		ac.setLastUpdateBy(user.getUseId());
		// ac.setLastUpdateDate(new Date());
		sysInformService.modifyAppAonnce(ac);
		return "redirect:/appManager/appInformList";
	}

	/**
	 * app公告关闭
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "关闭app公告", type = LogType.DELETE)
	@RequiresPermissions("app:appAnnouce:delete")
	@ResponseBody
	@RequestMapping(value = "/{advId}/appAnnouceClose")
	public String appAnnouceClose(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		// Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		// ac.setStatus(Constants.IS_VALID_FALSE);
		// ac.setStartDate(new Date());
		// ac.setLastUpdateBy(user.getUseId());
		sysAnnounceservice.deleteAppAonnce(advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * app公告关闭
	 * 
	 * @param model
	 * @return
	 */
	@SystemLog(module = "app管理", operation = "关闭app通知", type = LogType.DELETE)
	@RequiresPermissions("app:appInform:delete")
	@ResponseBody
	@RequestMapping(value = "/{advId}/appAInformClose")
	public String appAInformClose(@CurrentUser User user, Model model,
			Advice advice) throws ParseException {

		// 更新公告表
		// Advice ac = sysAnnounceservice.findDetailById(advice.getAdvId());
		// ac.setStatus(Constants.IS_VALID_FALSE);
		// ac.setStartDate(new Date());
		// ac.setLastUpdateBy(user.getUseId());
		sysInformService.deleteAppAonnce(advice);
		return Constants.DEFAULT_MSG_SUCCESS;
	}

	/**
	 * 查询App帐号
	 * 
	 * @param user
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("findAppUserList")
	public String findAppUserList(User user, Page page, Model model) {
		user.setUseType(1);
		PageReturn<User> result = appService.findAppUserList(user, page);
		model.addAttribute("result", result);
		return "app/appUserList";
	}

	/**
	 * 查询用户详情
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping("findAppUserInfo")
	public String findAppUserInfo(User user, Model model) {
		User result = appService.findAppUserInfo(user);
		model.addAttribute("result", result);
		return "app/appUserDtail";
	}

	/**
	 * 查询版本列表
	 * 
	 * @param user
	 * @param page
	 * @param model
	 * @return
	 */
	@RequestMapping("findVersionList")
	public String findVersionList(SoftwareVer softwareVer, Page page,
			Model model) {
		PageReturn<SoftwareVer> result = appService.findVersionList(
				softwareVer, page);
		model.addAttribute("result", result);
		return "app/appVersionList";
	}

	/**
	 * 跳转到新增页面
	 * 
	 * @param softwareVer
	 * @param model
	 * @return
	 */
	@RequestMapping("addAppVersion")
	public String addAppVersion(SoftwareVer softwareVer, Model model) {
		return "app/addAppVersion";
	}

	/**
	 * 保存版本信息
	 * 
	 * @param softwareVer
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "saveAppVersion", method = RequestMethod.POST)
	public int saveAppVersion(@CurrentUser User user, SoftwareVer softwareVer) {
		softwareVer.setVerId(IdWorkerFactory.nextId());
		softwareVer.setInsertBy(user.getUseId());
		softwareVer.setLastUpdateBy(user.getUseId());
		return appService.insertSelective(softwareVer);
	}

	/**
	 * 查询单条记录
	 * 
	 * @param softwareVer
	 * @param model
	 * @return
	 */
	@RequestMapping("findAppVersion")
	public String findAppVersion(SoftwareVer softwareVer, Model model) {
		model.addAttribute("result", appService.findAppVersion(softwareVer));
		return "app/editAppVersion";
	}

	/**
	 * 保存版本信息
	 * 
	 * @param softwareVer
	 * @return
	 */
	@ResponseBody
	@RequestMapping("updateAppVersion")
	public int updateAppVersion(@CurrentUser User user,
			SoftwareVer softwareVer, Model model) {
		softwareVer.setLastUpdateBy(user.getUseId());
		return appService.updateAppVersion(softwareVer);
	}
}
